import {Connect} from './url'
import {timestampToCommonDate} from "@/utils/formatTime"
import { _array } from "@/utils/arrayExtension";
import Vue from 'vue'
/**
 * @Description: ${wx请求数据}
 * @Param
 * @Return
 * @Author 焦政
 * @Date 2018/11/21
 * @Version 1.0
 */
const formData = params => new Promise((resolve, reject) => {
    wx.showNavigationBarLoading()
    const beforeRequest = new Date().getTime()
    wx.request({
        url:  `${Connect.baseUrl}${params.url}`,
        data: params.data,
        header: {'content-type': 'application/x-www-form-urlencoded', ...params.header},
        method: params.method,
        success: function(res) {
            let isSuccess = false
            if (parseInt(res.statusCode / 100) == 2) {
                if (res.data.code == 0) {
                    isSuccess = true
                }
            }

            let response = res.data
            response.statusCode = res.statusCode
            const pages = getCurrentPages()
            const currentPage = pages[pages.length - 1]
            const route = currentPage.route
            const networkType = wx.getStorageSync('networkType')
            const afterRequest = new Date().getTime()
            const timeDif = afterRequest - beforeRequest
            var networkArr = wx.getStorageSync('networkArr') || []
            if (networkArr.length >= 30) {
                wx.removeStorageSync('networkArr')
                networkArr = []
            }
            var time = timestampToCommonDate(new Date().getTime())
            networkArr = _array.unshift(networkArr, {url: params.url, params: params.data, res: response, time, route, timeDif, networkType, isSuccess})
            wx.setStorageSync('networkArr', networkArr)

            // if (!isSuccess) {
            //     wx.showToast({
            //         title: res.data.message,
            //         icon: 'none',
            //         duration: 4000
            //     })
            // }
            wx.hideNavigationBarLoading()
            resolve(res.data)
        },
        fail: function(e) {
            const pages = getCurrentPages()
            const currentPage = pages[pages.length - 1]
            const route = currentPage.route
            const networkType = wx.getStorageSync('networkType')
            const afterRequest = new Date().getTime()
            const timeDif = afterRequest - beforeRequest
            var networkArr = wx.getStorageSync('networkArr') || []
            if (networkArr.length >= 30) {
                wx.removeStorageSync('networkArr')
                networkArr = []
            }
            var time = timestampToCommonDate(new Date().getTime())
            networkArr = _array.unshift(networkArr, {url, params, res: e, time, route, timeDif, networkType, isSuccess: false})
            wx.setStorageSync('networkArr', networkArr)

            // wx.showToast({
            //     title: e.errMsg,
            //     icon: 'none',
            //     duration: 4000
            // })
            wx.hideNavigationBarLoading()
            reject(e)
        }
    })
})
const request = params => new Promise((resolve, reject) => {
    wx.showNavigationBarLoading()
    const beforeRequest = new Date().getTime()
    wx.request({
        url:  `${Connect.baseUrl}${params.url}`,
        data: params.data,
        header: {'Content-Type': 'application/json', ...params.header},
        method: params.method,
        success: function(res) {
            let isSuccess = false
            if (parseInt(res.statusCode / 100) == 2) {
                if (res.data.code == 0) {
                    isSuccess = true
                }
            }

            let response = res.data
            response.statusCode = res.statusCode
            const pages = getCurrentPages()
            const currentPage = pages[pages.length - 1]
            const route = currentPage.route
            const networkType = wx.getStorageSync('networkType')
            const afterRequest = new Date().getTime()
            const timeDif = afterRequest - beforeRequest
            var networkArr = wx.getStorageSync('networkArr') || []
            if (networkArr.length >= 30) {
                wx.removeStorageSync('networkArr')
                networkArr = []
            }
            var time = timestampToCommonDate(new Date().getTime())
            networkArr = _array.unshift(networkArr, {url: params.url, params: params.data, res: response, time, route, timeDif, networkType, isSuccess})
            wx.setStorageSync('networkArr', networkArr)

            // if (!isSuccess) {
            //     wx.showToast({
            //         title: res.data.message,
            //         icon: 'none',
            //         duration: 4000
            //     })
            // }
            wx.hideNavigationBarLoading()
            resolve(res.data)
        },
        fail: function(e) {
            const pages = getCurrentPages()
            const currentPage = pages[pages.length - 1]
            const route = currentPage.route
            const networkType = wx.getStorageSync('networkType')
            const afterRequest = new Date().getTime()
            const timeDif = afterRequest - beforeRequest
            var networkArr = wx.getStorageSync('networkArr') || []
            if (networkArr.length >= 30) {
                wx.removeStorageSync('networkArr')
                networkArr = []
            }
            var time = timestampToCommonDate(new Date().getTime())
            networkArr = _array.unshift(networkArr, {url, params, res: e, time, route, timeDif, networkType, isSuccess: false})
            wx.setStorageSync('networkArr', networkArr)

            // wx.showToast({
            //     title: e.errMsg,
            //     icon: 'none',
            //     duration: 4000
            // })
            wx.hideNavigationBarLoading()
            reject(e)
        }
    })
})
export {request,formData}
Vue.prototype.$JZWX = request
Vue.prototype.$JZFormdata = formData